<template>
  <div>
    <el-button
      type="primary"
      size="mini"
      icon="el-icon-plus"
      @click="showAddDialog()"
      >添加</el-button
    >
    <el-table
      ref="multipleTable"
      :data="list"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="ID"> </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="code" label="编号"> </el-table-column>
      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="parmary"
            icon="el-icon-edit"
            @click="showUpdateDialog(scope.row.id)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="selectByPage"
      :current-page="page"
      :page-size="limit"
      layout="total, prev, pager, next"
      :total="total"
    >
    </el-pagination>

    <!-- 添加、编辑的dialog -->
    <el-dialog title="添加/编辑" :visible.sync="dialogVisible" width="40%">
      <el-form ref="form" :model="role" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="role.name"></el-input>
        </el-form-item>
        <el-form-item label="编号">
          <el-input v-model="role.code"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="role.remark"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addOrUpdate">提交</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
  
<script>
//引入role.js
import roleApi from "@/api/rbac/role";

export default {
  name: "",
  data() {
    return {
      list: null, //当前页的数据
      page: 1, //当前页
      limit: 5, //每页有多少条
      total: 0, //总记录数

      //添加、编辑相关
      dialogVisible: false, //控制添加、修改的dialog是不是显示
      role: {}, //添加、修改对应封装的role对象
    };
  },
  created() {
    this.selectByPage(this.page);
    /*roleApi.selectByPage(this.page, this.limit).then(result => {
      console.log(result)
      this.list = result.data.list
      this.total = result.data.total
    })*/
  },
  methods: {
    selectByPage(page = 1) {
      //页面切换时候会传递过来第几页
      this.page = page;
      roleApi.selectByPage(this.page, this.limit).then((result) => {
        console.log(result);
        this.list = result.data.list;
        //Invalid prop: type check failed for prop "total". Expected Number with value 8, got String with value "8".
        //把long类型转换成Number类型
        this.total = result.data.total - 0;
      });
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("您确认要删除么?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        roleApi.deleteById(row.id).then((result) => {
          if (result.ok) {
            this.$message({
              type: "success",
              message: result.msg,
            });
            //删除完了之后，重新查询最新的数据
            this.selectByPage();
          } else {
            this.$message({
              type: "error",
              message: result.msg,
            });
          }
        });
      });
    },
    showAddDialog() {
      this.dialogVisible = true;
      this.role = {};
    },
    showUpdateDialog(id) {
      this.dialogVisible = true;
      this.role = {};
      roleApi.selectById(id).then((result) => {
        this.role = result.data;
      });
    },
    addOrUpdate() {
      if (!this.role.id) {
        roleApi.add(this.role).then((result) => {
          if (result.ok) {
            this.$message({
              type: "success",
              message: result.msg,
            });
            //关闭弹出框
            this.dialogVisible = false;
            //查找最新数据
            this.selectByPage();
          } else {
            this.$message({
              type: "error",
              message: result.msg,
            });
          }
        });
      } else {
        roleApi.update(this.role).then((result) => {
          if (result.ok) {
            this.$message({
              type: "success",
              message: result.msg,
            });
            //关闭弹出框
            this.dialogVisible = false;
            //查找最新数据
            this.selectByPage();
          } else {
            this.$message({
              type: "error",
              message: result.msg,
            });
          }
        });
      }
    },
  },
};
</script>
 
<style scoped>
</style>